package com.bawei.module_service.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_service.R
import com.bawei.module_service.databinding.PollingItemBinding
import com.bawei.module_service.model.PatrolModelItem
import com.bawei.module_service.model.PollingModelItem

class PollingAdapter: RecyclerView.Adapter<PollingAdapter.PollingViewHolder>() {

    val pollingList= mutableListOf<PollingModelItem>()
    class PollingViewHolder (itemView:PollingItemBinding) : RecyclerView.ViewHolder(itemView.root)

    fun setData(data:MutableList<PollingModelItem>) {
        pollingList.clear()
        pollingList.addAll(data)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PollingViewHolder {
        val binding= PollingItemBinding.inflate(LayoutInflater.from(parent.context)
            ,parent,
            false)
        return PollingViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return pollingList.size
    }

    override fun onBindViewHolder(holder: PollingViewHolder, position: Int) {

        val binding=DataBindingUtil.getBinding<PollingItemBinding>(holder.itemView)
        if (pollingList[position].pointLocation.isNullOrEmpty()&&pollingList[position].pointName.isNullOrEmpty()&&pollingList[position].patrolType.isNullOrEmpty()&&pollingList[position].patrolStandard.isNullOrEmpty()){

         return

        }else{
            binding?.pointLocation?.setText("点位位置："+pollingList[position].pointLocation)
            if (pollingList[position].patrolType.equals("安房巡检")){

                binding?.patrolType?.setTextColor(Color.parseColor("#FFFFFF"))
                binding?.patrolType?.setText("安房巡检")
                binding?.patrolType?.background= ContextCompat.getDrawable(holder.itemView.context,
                    R.drawable.service_rounded_corners4)


            }else if (pollingList[position].patrolType.equals("保洁巡检")){
                binding?.patrolType?.setTextColor(Color.parseColor("#FFFFFF"))
                binding?.patrolType?.setText("保洁巡检")
                binding?.patrolType?.background= ContextCompat.getDrawable(holder.itemView.context,
                    R.drawable.service_rounded_corners5)

            }else{
                binding?.patrolType?.setTextColor(Color.parseColor("#FFFFFF"))
                binding?.patrolType?.setText(pollingList[position].patrolType)
                binding?.patrolType?.background= ContextCompat.getDrawable(holder.itemView.context,
                    R.drawable.service_rounded_corners6)
            }

            if (pollingList[position].patrolStandard.equals("1")){
                binding?.patrolStandard?.text="点位状态：启用"
            }else{
                binding?.patrolStandard?.text="点位状态：未启用"
            }
            binding?.nameAddress?.setText("项目名称/"+pollingList[position].pointName)
        }

    }
}